<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'/wewant1/includes/img_process/cropProductImg.php');
	require_once($_SERVER['DOCUMENT_ROOT'].'/wewant1/includes/img_process/downloadImg.php');
	require_once($_SERVER['DOCUMENT_ROOT'].'/wewant1/includes/db_exe/db_connection.php');
	require_once($_SERVER['DOCUMENT_ROOT'].'/wewant1/includes/img_process/convert2JPG.php');
?>
<?php
	if ($_SERVER['REQUEST_METHOD'] == 'POST'){
		// originalImgUrl
		// productUrl
		// selectedCollection
		// titleProduct
		// priceProduct
		// genderProduct

		//mo ket noi voi database
		$productName = mysql_real_escape_string($_POST['titleProduct']);
		$productPrice = mysql_real_escape_string($_POST['priceProduct']);
		$productUrl = mysql_real_escape_string($_POST['productUrl']);
		$idCollection = mysql_real_escape_string($_POST['selectedCollection']);
		//$idSrcImg chinh la id hay cung la img_src cua product
		$idShop = 0;
		$shopUrl = parse_url($productUrl,1);
		//kiem tra shopUrl va loai bo www. trong link shop
		if(strlen($shopUrl)>4){
			//neu shopUrl co nhieu hon 4 ky tu thi moi tien hanh kiem tra
			if(substr($shopUrl, 0,4) === 'www.'){
				//neu shopUrl co dang www. thì cat bo phan www. di
				$shopUrl = substr($shopUrl,4);
			}
		}


		$dbConn = connectDb();
		//query select shop co url trung voi url daco trong database, neu khong co thi tao moi
		$q = "SELECT id_shop FROM shops WHERE shop_url='{$shopUrl}' LIMIT 1";

		if(mysqli_num_rows($r = exeQuery($dbConn,$q)) == 1){
			
			//neu co thi gan bien idShop cho id cua shop tim duoc
			$row = mysqli_fetch_array($r);
			//gan cho idShop vao ket qua tra ve
			$idShop = $row[0];
			
		} else{
			//lay domain site cua shop lam shop_username va link shop tren site sau nay

			

			//neu khong thi INSERT 1 shop moi vo trong db
			$q = "INSERT INTO shops (shop_url) VALUES ('{$shopUrl}')";
			//thuc hien cau lenh truy van insert
			exeQuery($dbConn,$q);
			//lay id auto increment cua shop tao duoc
			$idShop = mysqli_insert_id($dbConn);

		}//end if insert shop moi neu khong co
		
		//tao id cho img de luu vao file tmp va xu ly
		$idSrcImg = uniqid(time());

		$q = "INSERT INTO products (product_name,product_price,product_url,image_src,id_shop,id_user_upload,product_gender,created_date) VALUES ('{$productName}',$productPrice,'{$productUrl}','{$idSrcImg}',$idShop,1,{$_POST['genderProduct']},NOW())";
		exeQuery($dbConn,$q);

		//lay id cua product vua moi tao de luu vao bang user_collected
		$idProduct = mysqli_insert_id($dbConn);

		//kiem tra neu Insert sp moi thanh cong 
		if(mysqli_affected_rows($dbConn) == 1){

			//kiem tra type cua product de xu ly tao duong dan png hoac jpg, dong thoi de crop hinh anh sau nay
			if(strtolower(substr($_POST['originalImgUrl'], -4))=='.png'){
				$typeImg = '.png';
			} else{
				$typeImg = '.jpg';
			}

			
			//source cua file tam thoi se download ve
			$srcImgTmp	= $_SERVER['DOCUMENT_ROOT'].'/wewant1/upload/tmp_images/'.$idSrcImg.$typeImg;


			//download va crop file img
			if(downloadImg($_POST['originalImgUrl'],$srcImgTmp)){

				if($typeImg == '.png') {
					convert2JPG($srcImgTmp, $_SERVER['DOCUMENT_ROOT'].'/wewant1/upload/tmp_images/'.$idSrcImg.'.jpg');//chuyen doi xong
					unlink($srcImgTmp);//xoa file img png da duoc chuyen doi xong
					$srcImgTmp = $_SERVER['DOCUMENT_ROOT'].'/wewant1/upload/tmp_images/'.$idSrcImg.'.jpg' ;//tao lai duong dan tam thoi la img jpg sau khi chuyen doi
				}

				if(cropProductImg($srcImgTmp,$idSrcImg)){
					//neu ok thi move file img original ve thu muc chua anh
					rename($_SERVER['DOCUMENT_ROOT'].'/wewant1/upload/tmp_images/'.$idSrcImg.'.jpg',$_SERVER['DOCUMENT_ROOT'].'/wewant1/upload/image_product/'.$idSrcImg.'.jpg');

					//Tiep tuc query insert table user collected
					//tao id_collected cho action collec. ID nay gom md5(id_user-id_product-id_collection)/13 chu
					$idCollected = substr(md5('1'.'-'.$idProduct.'-'.$idCollection),0,13);
					$q = "INSERT INTO user_collected (id_collected,id_product,id_user,id_collection,created_date) VALUES ('{$idCollected}',$idProduct,1,$idCollection,NOW())";

					exeQuery($dbConn,$q);
					if(mysqli_affected_rows($dbConn) == 1){
						echo "da thanh cong";
					} else {
						echo "khong them vao table user_collected duoc";
					}

				} else {
					echo "khong crop duoc";
				}
			} else {
				echo "khong download file hinh duoc";
			}//end if ko download duoc anh

		}// end if kiem tra ham insert table products

		mysqli_close($dbConn);
	}
?>